import type { PortalProps } from "@/components/Component/Portal";

export const getAttach = (attach: PortalProps["attach"]) => {
  if (typeof attach === "string") {
    return document.querySelector(attach);
  }
  if (typeof attach === "object" && attach instanceof window.HTMLElement) {
    return attach;
  }
  return document.body;
};
/**
 *
 * @param obj
 * @description 判断是否为数字
 *
 * @returns
 */
export const isNumber = (obj: any): obj is number => {
  return (
    Object.prototype.toString.call(obj) === "[object Number]" && obj === obj
  );
};
/**
 *
 * @param value
 * @param defaultValue
 * @description 转为数字
 *
 * @returns
 */
export const toNumber = (value: number | string, defaultValue?: number) => {
  if (value === undefined) {
    return defaultValue;
  }
  if (isNumber(value)) {
    return value;
  }
  const numberVal = parseFloat(value);
  return isNaN(numberVal) ? defaultValue : numberVal;
};
